﻿<script lang="ts" name="inboundmanagement" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { useInboundmanagementApi } from '/@/api/DiscrepancyManage/inboundmanagement';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const inboundmanagementApi = useInboundmanagementApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
  inboundDetails: [{required: true, message: '请选择入库明细！', trigger: 'blur',},],
  inboundQuantity: [{required: true, message: '请选择入库数量！', trigger: 'blur',},],
  inboundPrice: [{required: true, message: '请选择入库单价！', trigger: 'blur',},],
});

// 页面加载时
onMounted(async () => {
  const data = await inboundmanagementApi.getDropdownData(false).then(res => res.data.result) ?? {};
  state.dropdownData.inboundTypeId = data.inboundTypeId ?? [];
  state.dropdownData.repositoryNameId = data.repositoryNameId ?? [];
  state.dropdownData.trackingNumber = data.trackingNumber ?? [];
  state.dropdownData.vendor = data.vendor ?? [];
  state.dropdownData.administrator = data.administrator ?? [];
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {  };
	state.ruleForm = row.id ? await inboundmanagementApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await inboundmanagementApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="inboundmanagement-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库单号" prop="inboundNumber">
							<el-input v-model="state.ruleForm.inboundNumber" placeholder="请输入入库单号" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库类型" prop="inboundTypeId">
							<el-select clearable filterable v-model="state.ruleForm.inboundTypeId" placeholder="请选择入库类型">
								<el-option v-for="(item,index) in state.dropdownData.inboundTypeId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="仓库名称" prop="repositoryNameId">
							<el-select clearable filterable v-model="state.ruleForm.repositoryNameId" placeholder="请选择仓库名称">
								<el-option v-for="(item,index) in state.dropdownData.repositoryNameId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库日期" prop="inboundDate">
							<el-date-picker v-model="state.ruleForm.inboundDate" type="date" placeholder="入库日期" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="关联单号" prop="trackingNumber">
							<el-select clearable filterable v-model="state.ruleForm.trackingNumber" placeholder="请选择关联单号">
								<el-option v-for="(item,index) in state.dropdownData.trackingNumber" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="供应商" prop="vendor">
							<el-select clearable filterable v-model="state.ruleForm.vendor" placeholder="请选择供应商">
								<el-option v-for="(item,index) in state.dropdownData.vendor" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="联系人" prop="contacts">
							<el-input v-model="state.ruleForm.contacts" placeholder="请输入联系人" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="联系方式" prop="contact">
							<el-input v-model="state.ruleForm.contact" placeholder="请输入联系方式" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="经办人" prop="administrator">
							<el-select clearable filterable v-model="state.ruleForm.administrator" placeholder="请选择经办人">
								<el-option v-for="(item,index) in state.dropdownData.administrator" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="备注" prop="remark">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注" maxlength="200" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="上传附件" prop="uploadAttachments">
							<el-input v-model="state.ruleForm.uploadAttachments" placeholder="请输入上传附件" maxlength="2000" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库明细" prop="inboundDetails">
							<el-input v-model="state.ruleForm.inboundDetails" placeholder="请输入入库明细" maxlength="1000" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库数量" prop="inboundQuantity">
							<el-input-number v-model="state.ruleForm.inboundQuantity" placeholder="请输入入库数量" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库单价" prop="inboundPrice">
							<el-input-number v-model="state.ruleForm.inboundPrice" placeholder="请输入入库单价" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>